package com.engine.zzworkflow.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.zzworkflow.service.Impl.SendBusinessServiceImpl;
import com.engine.zzworkflow.service.SendBusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;


public class SendBusinessAction {

    private SendBusinessService getService() {
        return ServiceUtil.getService(SendBusinessServiceImpl.class);
    }

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 抛送单据数据到会计引擎生成凭证
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/sendbusiness")
    @Produces({MediaType.TEXT_PLAIN})
    public String sendBusiness(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            Map params = ParamUtil.request2Map(request);
            User user=HrmUserVarify.getUser(request,response);
            logger.info("抛送单据数据到会计引擎生成凭证start...params:"+params);
            String res = JSONObject.toJSONString(getService().sendBusiness(user,params));
            logger.info("抛送单据数据到会计引擎生成凭end...res:"+res);
            return res;
        } catch (Exception e) {
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            logger.error("抛送单据数据到会计引擎生成凭证异常...err:"+e.getMessage(), e);
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 计引擎生回写凭证明细
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/savevouchers")
    @Produces({MediaType.APPLICATION_JSON})
    public String saveBusinessVouchers(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                       String params){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            logger.info("抛送单据数据到会计引擎生成凭证start...params:"+params);
            params = Util.null2String(params);
            if("".equals(params)) {
                apidatas.put("code", -1);
                apidatas.put("msg", "抛送单据数据到会计引擎生成凭证参数不能为空");
            }
            JSONObject paramObj = JSONObject.parseObject(params);
            String res = JSONObject.toJSONString(getService().saveBusinessVouchers(paramObj));
            logger.info("抛送单据数据到会计引擎生成凭证end...res:"+res);
            return res;
        } catch (Exception e) {
            apidatas.put("code", -3);
            apidatas.put("msg", "catch exception : " + e.getMessage());
            logger.error("抛送单据数据到会计引擎生成凭证异常...err:"+e.getMessage(), e);
        }
        return JSONObject.toJSONString(apidatas);
    }


}
